home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Lib / table / tb_dbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  4.7 KB  |  213 lines

  1. /* tb_dbm.c: table lookup routines ([n]dbm based) */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Lib/table/RCS/tb_dbm.c,v 6.0 1991/12/18 20:24:28 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Lib/table/RCS/tb_dbm.c,v 6.0 1991/12/18 20:24:28 jpo Rel $
  9.  *
  10.  * $Log: tb_dbm.c,v $
  11.  * Revision 6.0  1991/12/18  20:24:28  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. #include "head.h"
  19. #include "dbase.h"
  20. #include "table.h"
  21. #include "sys.file.h"
  22.  
  23. extern char *ppdbm, *tbldfldir;
  24. extern    void    getfpath ();
  25. extern    void    err_abrt ();
  26.  
  27. #ifndef       GCC_DBM_OK
  28.       /* GCC_DBM_OK should be declared if you KNOW that your (n)dbm library
  29.        * returns structures in the same was as gcc exxpects them.
  30.        */
  31. # ifdef       __GNUC__
  32. #ifdef sparc
  33.  #error GCC and sparc architecture dont get on very well with structures compile this file with cc
  34. #endif
  35.       /* most pcc based compilers screw up the return of structures.
  36.        * They actually return a pointer to a static struct, which is copied.
  37.        * Here is a HACK to get such a pcc version of the library to work
  38.        * with the gnu structure returning convention. NB: need a cpp which
  39.        * allows recursive definitions (such as gcc)
  40.        */
  41. #  ifdef NDBM
  42. #   ifndef dbm_fetch
  43.  #    error You are using gcc with ndbm, but have not fixed <ndbm.h>
  44.  #    error (or defined GCC_DBM_OK if the dbm library is OK asis)
  45. #   endif  /* dbm_fetch */
  46. #  else       /* NDBM */
  47. #   ifndef fetch
  48.  #    error You are using gcc with dbm, but have not fixed <dbm.h>
  49.  #    error (or defined GCC_DBM_OK if the dbm library is OK asis)
  50. #   endif  /* fetch */
  51. #  endif /* NDBM */
  52. # endif /* __GNUC__ */
  53. #endif /* GCC_DBM_OK */
  54.  
  55.  
  56. /* ---------------------  Begin  Routines  -------------------------------- */
  57.  
  58. static int tb_fetch ();
  59.  
  60. /* -- find the value (address), given its key (hostname) -- */
  61.  
  62. int tb_dbmk2val (table, name, buf, first)
  63. register Table          *table;
  64. char                    name[],   /* -- name of ch "member" / key -- */
  65.             *buf;     /* -- put value in this buffer -- */
  66. int    first;
  67. {
  68.     static Dbvalue         dbm;
  69.     static Dbptr  dp = NULL;
  70.     register char   *cp;
  71.  
  72.     if (first) {
  73.         if (tb_fetch (name, dbm))
  74.             dp = dbm;
  75.         else dp = NULL;
  76.     }
  77.     else if (dp)
  78.         dp ++;
  79.  
  80.     for (; dp && (cp = dp->db_table) != NULLCP; dp++) {
  81.         if (lexequ (cp, table -> tb_name) != 0)
  82.             continue;
  83.         if (buf != 0) {
  84.             if (dp->db_value == NULL)
  85.                 *buf = '\0';
  86.             else
  87.                 (void) strcpy (buf, dp->db_value);
  88.         }
  89.         return (OK);
  90.     }
  91.     if (buf != 0)
  92.         (void) strcpy (buf, " (ERROR)");
  93.  
  94.     return (NOTOK);
  95. }
  96.  
  97.  
  98.  
  99. /* ---------------------  Static  Routines  ------------------------------- */
  100.  
  101.  
  102.  
  103. static int tb_fetch (name, dbm)
  104. char            *name;     /* -- use this key to fetch entry  -- */
  105. Dbvalue         dbm;       /* -- put the entry here -- */
  106. {
  107.     static char     dbvalue[ENTRYSIZE];
  108.     register Dbptr  dp;
  109.     datum           key,
  110.             value;
  111.     register char   *p,
  112.             *cp;
  113.     int             cnt;
  114.  
  115. #ifdef GDBM
  116.     static GDBM_FILE thedb;
  117. #else
  118. #ifdef NDBM
  119.     static DBM      *thedb;
  120. #else
  121.     static int    savedirf, savepagf;
  122.     int    tdirf, tpagf;
  123. #endif
  124. #endif
  125.     static int      dbmopen = FALSE;
  126.  
  127.  
  128.        if (dbmopen == FALSE) {
  129.         char    filename [BUFSIZ];
  130.  
  131.         getfpath (tbldfldir, ppdbm, filename);
  132.  
  133. #ifdef GDBM
  134.         (void) strcat (filename, ".gdbm");
  135.         if ((thedb = gdbm_open (filename, 0, GDBM_READER, 0, NULL)) == NULL)
  136.              err_abrt (RP_FIO, "Error opening database '%s`",
  137.                    filename);
  138. #else
  139. #ifdef NDBM
  140.         if ((thedb = dbm_open (filename, O_RDONLY, 0)) == NULL)
  141.             err_abrt (RP_FIO,
  142.                  "Error opening database '%s'", filename);
  143. #else
  144.         if (dbminit (filename) < 0)
  145.             err_abrt (RP_FIO,
  146.                  "Error opening database '%s'", filename);
  147.         savedirf = dirf;
  148.         savepagf = pagf;
  149. #endif
  150. #endif
  151.         dbmopen = TRUE;
  152.     }
  153.  
  154.  
  155.     key.dptr = dbvalue;
  156.     key.dsize = strlen (name) + 1;
  157.     for (cp = dbvalue, p = name; *p; p++)
  158.         *cp++ = uptolow (*p);
  159.     *cp = 0;
  160.  
  161.  
  162. #ifdef GDBM
  163.         value = gdbm_fetch (thedb, key);            
  164. #else
  165. #ifdef NDBM
  166.     value = dbm_fetch (thedb, key);
  167. #else
  168.     tpagf = pagf; tdirf = dirf;
  169.     pagf = savepagf; dirf = savedirf;
  170.     value = fetch (key);
  171.     pagf = tpagf; dirf = tdirf;
  172. #endif
  173. #endif
  174.  
  175.  
  176.     if (value.dptr == NULL
  177. #ifdef NDBM
  178.         || dbm_error (thedb)
  179. #endif NDBM
  180.         )
  181.     {
  182. #ifdef NDBM
  183.         dbm_clearerr (thedb);
  184. #endif NDBM
  185.  
  186.         PP_DBG (("Lib/tb_fetch/FALSE (%s, %d)",
  187.                 key.dptr, key.dsize));
  188.         return (FALSE);
  189.     }
  190.  
  191.  
  192.  
  193.     (void) strcpy (dbvalue, value.dptr);
  194.  
  195.     cnt = MAXDBENTRIES-1;
  196.     for (p = dbvalue, dp = dbm; *p && cnt ; cnt--, dp++) {
  197.         for (dp->db_table = p ; *p ; p++)
  198.             if (*p == ' ') {      /* get table name */
  199.                 *p++ = '\0';
  200.                 break;
  201.             }
  202.         for (dp->db_value = (*p ? p : NULL); *p; p++)
  203.             if (*p == FS) {      /* get value-part */
  204.                 *p++ = '\0';
  205.                 break;
  206.         }
  207.     }
  208.     for (; cnt ; cnt--, dp++)
  209.         dp->db_table = dp->db_value = NULL;
  210.  
  211.     return (TRUE);
  212. }
  213.